package pos.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import pos.domain.Mensaje;
import pos.domain.Usuario;
import pos.utils.UIDGenerator;

public class JDBCMensajeDAO implements IMensajeDAO{
	IUsuarioDAO udao;
	private Connection conn;
	
	public JDBCMensajeDAO()
	{
		conn = ConnectionManager.getInstance().checkOut();
	}
	public JDBCMensajeDAO(Connection conn)
	{
		this.conn = conn;
	}
	
	public Mensaje selectMensaje(Connection cnn, String idMensaje){
		PreparedStatement stmt = null;
		ResultSet result = null;
		
		Mensaje msg = new Mensaje();
		String sql = "SELECT * FROM Mensajes WHERE (OID = ?)";
		
		try{
			stmt = cnn.prepareStatement(sql);
			stmt.setString(1, idMensaje);
			result = stmt.executeQuery();
			
			result.next();
			msg = new Mensaje();
			msg.setIdMensaje(result.getString("OID"));
			msg.setMen(result.getString("texto"));
			System.out.println(result.getString("texto"));
			Usuario u = udao.selectUsuario( result.getString("userName"));
			msg.setCreador(u);
		}catch (SQLException e){
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		}finally {
			try {
				if(result != null){
					result.close();
				}
				if (stmt != null){
					stmt.close();
				}
			}catch (SQLException e){}
		}
		return msg;
	}
	
	public List<Mensaje> selectListaMensaje(Connection cnn, String idEvento){
		List l = new ArrayList();
		PreparedStatement stmt = null;
		ResultSet result = null;
		String sql = "SELECT * FROM mensaje M, evento E WHERE M.eventoOID = E.eventoOID AND (E.eventoOID = ?)";
		
		try {
			stmt = cnn.prepareStatement(sql);
			stmt.setString(1, idEvento);
			
			while (result.next()){
				Mensaje msj = selectMensaje(cnn, result.getString("idMensaje"));
				l.add(msj);
			}
		}catch (SQLException e){
			System.out.println ("Message" + e.getMessage());
			System.out.println ("SQLState: " + e.getSQLState());
			System.out.println ("ErrorCode: " + e.getErrorCode());
		}finally {
			try{
				if (result != null){
					result.close();
				}
				if (stmt != null){
					stmt.close();
				}
			}catch (SQLException e){
			}
		}
		return l;
	}
	
	public boolean insertMensaje(Connection cnn, Mensaje msg, String idEvento, String userName){
		String sql = "INSERT INTO mensajes ( OID, texto, titulo, eventoOID, usuarioOID) VALUES (?, ?, ?, ?, ?) ";
        PreparedStatement stmt = null;
        boolean aux = true;
        String oid = UIDGenerator.getInstance().getKey(); 

        try {
            stmt = cnn.prepareStatement(sql);

            stmt.setString(1, oid);
            stmt.setString(2, msg.getMen());
            stmt.setString(3, msg.getTitulo());
            stmt.setString(4, idEvento);
            stmt.setString(5, userName);
            
            stmt.executeUpdate();

        } catch (SQLException e) {
        	aux = false;
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            	aux = false;
            }
        }
        return aux;
	}
}
